// pages/address/address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  onShow:function(){

    this.getAddress()
    
  },
  getAddress:function(){

    wx.showLoading({
      title: '加载中...',
    })

    wx.cloud.callFunction({
      name:'get_address',
      success:res=>{
        wx.hideLoading()
        console.log('res==>',res)
        this.setData({
          address:res.result.data
        })
      },
      fail:error=>{
        wx.hideLoading()
        console.log("云函数调用失败==>",error)
      }
    })



  },
  removeAddress:function(e){

    wx.showLoading({
      title: '加载中...',
    })

    wx.cloud.callFunction({
      name:'remove_address',
      data:{
        id:e.currentTarget.dataset.id
      },
      success:res=>{
        wx.hideLoading()
        console.log('删除成功==>',res)
      },
      fail:erro=>{
        wx.hideLoading()
        console.log('云函数调用失败 erro==>',erro)
      }
    })

    var address = this.data.address

    for(var i = 0; i < address.length ; i++){

      if(address[i]._id == e.currentTarget.dataset.id){

        address.splice(i,1)

        break;

      }

    }

    this.setData({
      address:address
    })


  },
  toNew:function(){

    wx.navigateTo({
      url: '../new/new?id=0',
    })

  },
  editAddress:function(e){

    wx.navigateTo({
      url: '../new/new?id=' + e.currentTarget.dataset.id,
    })


  }

  
})